package edu.eece261.fall11.server;

import java.util.LinkedList;

import com.google.appengine.api.datastore.Key;

import edu.eece261.fall11.Messages;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;


@PersistenceCapable
public class ShopList {
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
    
    @Persistent
    long listGlobalID;
    
    @Persistent
    private long listTimestamp;
    
    @Persistent
	private String listTitle;
    
    @Persistent
    private String listOwner;
    
    @Persistent
    private LinkedList<String> listItems;
    
    @Persistent
    private LinkedList<String> listUpdaters;
    
    @Persistent
    private LinkedList<Long> listTimestamps;
    
    @Persistent
    private LinkedList<String> listFollowers;
    
    @Persistent
    private String listLastUpdater;
    
    @Persistent
    private String listPassword;
    
    @Persistent
    private int listIsPrivate;
	
    @Persistent
    private int listFinished;
    
    public ShopList(Messages.ShopList list, boolean createGlobalID) {
    	update(list);
    	if(createGlobalID)
    		this.listGlobalID = System.currentTimeMillis();
    	else
    		this.listGlobalID = list.getGlobal_id();
    	this.listFinished = 0;
    	this.listFollowers = list.getFollowers();
    	this.listTimestamp = System.currentTimeMillis();
    }
    
    public ShopList(String title, long globalID, int finished) {
    	if(globalID == -1)
    		listGlobalID = System.currentTimeMillis();
    	else
    		listGlobalID = globalID;
    	listFinished = finished;
    	
    }
    
    public ShopList(ShopList list) {
		listGlobalID = list.listGlobalID;
		listFinished = list.listFinished;
		listFollowers = list.listFollowers;
		listLastUpdater = list.listLastUpdater;
		listOwner = list.listOwner;
		listItems = list.listItems;
		listUpdaters = list.listUpdaters;
		listTimestamps = list.listTimestamps;
		listTitle = list.listTitle;
		listPassword = list.listPassword;
		listIsPrivate = list.listIsPrivate;
		listTimestamp = System.currentTimeMillis();
	}

	public Key getKey() {
        return key;
    }

	public Messages.ShopList getMessagesList() {
		Messages.ShopList rtnValue = new Messages.ShopList()
			.setGlobal_id(listGlobalID)
			.setTitle(listTitle)
			.setTimestamp(listTimestamp)
			.setOwner(listOwner)
			.setItems(listItems)
			.setUpdaters(listUpdaters)
			.setTimestamps(listTimestamps)
			.setFollowers(listFollowers)
			.setLastUpdater(listLastUpdater)
			.setPassword(listPassword)
			.setIsPrivate(listIsPrivate)
			.setFinished(listFinished);
		return rtnValue;
	}

	public void update(Messages.ShopList list) {
		listGlobalID = list.getGlobal_id();
		listTitle = list.getTitle();
		listTimestamp = list.getTimestamp();
		listOwner = list.getOwner();
		listItems = list.getItems();
		listUpdaters = list.getUpdaters();
		listTimestamps = list.getTimestamps();
		listFollowers = list.getFollowers();
		listLastUpdater = list.getLastUpdater();
		listPassword = list.getPassword();
		listIsPrivate = list.getIsPrivate();
		listFinished = list.isFinished();
	}

	public long getListGlobalID() {
		return listGlobalID;
	}

	public ShopList setListGlobalID(long listGlobalID) {
		this.listGlobalID = listGlobalID;
		return this;
	}

	public long getListTimestamp() {
		return listTimestamp;
	}

	public ShopList setListTimestamp(long listTimestamp) {
		this.listTimestamp = listTimestamp;
		return this;
	}

	public String getListOwner() {
		return listOwner;
	}

	public ShopList setListOwner(String listOwner) {
		this.listOwner = listOwner;
		return this;
	}

	public String getListTitle() {
		return listTitle;
	}

	public ShopList setListTitle(String listTitle) {
		this.listTitle = listTitle;
		return this;
	}

	public LinkedList<String> getListItems() {
		return listItems;
	}

	public ShopList setListItems(LinkedList<String> listItems) {
		this.listItems = listItems;
		return this;
	}
	
	public ShopList addListItem(String Item, String updater) {
		this.listItems.add(Item);
		this.listUpdaters.add(updater);
		this.listTimestamps.add(System.currentTimeMillis());
		if (!this.listFollowers.contains(updater))
		{
			this.listFollowers.add(updater);
		}
		this.listLastUpdater = updater;
		return this;
	}
	
	public LinkedList<String> getListUpdaters() {
		return listUpdaters;
	}
	
	public ShopList setListUpdaters(LinkedList<String> updaters) {
		this.listUpdaters = updaters;
		return this;
	}
	
	public LinkedList<Long> getListTimestamps() {
		return this.listTimestamps;
	}
	
	public ShopList setListTimestamps(LinkedList<Long> timestamps) {
		this.listTimestamps = timestamps;
		return this;
	}

	public LinkedList<String> getListFollowers() {
		return listFollowers;
	}

	public ShopList setListFollowers(LinkedList<String> listFollowers) {
		this.listFollowers = listFollowers;
		return this;
	}
	
	public ShopList addListFollower(String follower) {
		if (!this.listFollowers.contains(follower)) {
			this.listFollowers.add(follower);
		}
		return this;
	}

	public String getListLastUpdater() {
		return listLastUpdater;
	}

	public ShopList setListLastUpdater(String listLastUpdater) {
		this.listLastUpdater = listLastUpdater;
		return this;
	}
	
	public String getListPassword() {
		return listPassword;
	}
	
	public ShopList setListPassword(String listPassword) {
		this.listPassword = listPassword;
		return this;
	}

	public int getListIsPrivate() {
		return listIsPrivate;
	}
	
	public ShopList setListIsPrivate (int listIsPrivate) {
		this.listIsPrivate = listIsPrivate;
		return this;
	}
	
	public int isListFinished() {
		return listFinished;
	}

	public ShopList setListFinished(int listFinished) {
		this.listFinished = listFinished;
		return this;
	}
	
	public ShopList copyTo(Messages.ShopList list) {
		list.setGlobal_id(listGlobalID);
		list.setTitle(listTitle);
		list.setTimestamp(listTimestamp);
		list.setOwner(listOwner);
		list.setItems(listItems);
		list.setFollowers(listFollowers);
		list.setLastUpdater(listLastUpdater);
		list.setPassword(listPassword);
		list.setIsPrivate(listIsPrivate);
		list.setFinished(listFinished);
		return this;
	}

	public ShopList editListItem(int index, String item, String updater) {
		this.listItems.set(index, item);
		this.listUpdaters.set(index, updater);
		this.listTimestamps.set(index, System.currentTimeMillis());
		if (!this.listFollowers.contains(updater))
		{
			this.listFollowers.add(updater);
		}
		this.listLastUpdater = updater;
		this.listTimestamp = System.currentTimeMillis();
		return this;
		
	}
}